package servlets.local;

import hibernate.method.TableEntityMethods;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Servlet implementation class addTable
 */
@WebServlet("/addTable")
public class addTable extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public addTable() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String local_id = request.getParameter("local");
		String value = request.getParameter("value");
		// String label = request.getParameter("label");
		String persons = request.getParameter("persons");
		String smoking = request.getParameter("smoking");

		PrintWriter out = response.getWriter();
		response.setContentType("text/html");

		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session s = sessionFactory.openSession();
		s.beginTransaction();

		TableEntityMethods tem = new TableEntityMethods();
		tem.addTable(sessionFactory, s, Integer.parseInt(local_id), value,
				value, Integer.parseInt(persons), smoking);

		try {
			s.getTransaction().commit();
			out.println("<script type=\"text/javascript\">alert(\"Stolik dodany\"); window.location.href = \"management/index.jsp?page=events&group=addTable\";</script>");
		} catch (Exception e) {
			out.println("<script type=\"text/javascript\">alert(\"Stolik nie dodany\"); window.location.href = \"management/index.jsp?page=events&group=addTable\";</script>");
		}
	}

}
